package com.xmmj.common.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.xmmj.common.room.dao.HistoryDao
import com.xmmj.common.room.dao.RecordDao
import com.xmmj.common.room.dao.AccountDao
import com.xmmj.common.room.entity.HistoryBean
import com.xmmj.common.room.entity.RecordBean
import com.xmmj.common.room.entity.AccountBean

/**
 * Room数据库
 */
@Database(
    entities = [RecordBean::class, HistoryBean::class, AccountBean::class],
    version = 1,
    exportSchema = true
)
abstract class AppDatabase : RoomDatabase() {

    abstract fun recordDao(): RecordDao

    abstract fun historyDao(): HistoryDao

    abstract fun accountDao(): AccountDao

    companion object {

        @JvmStatic
        @Volatile
        private var INSTANCE: AppDatabase? = null

        @JvmStatic
        fun getDatabase(context: Context): AppDatabase {
            if (INSTANCE == null) {
                synchronized(this) {
                    INSTANCE = Room.databaseBuilder(
                        context.applicationContext,
                        AppDatabase::class.java,
                        AppDatabase::class.java.name
                    ).build()
                }
            }
            return INSTANCE!!
        }
    }
}
